package com.example.dragandview.seniorUI;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.dragandview.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListviewActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.seniorui_listview);
//        String[] ctype = new String[]{"one","two","three","four","five"};
//        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ctype);
//        ListView listView = findViewById(R.id.id_listview);
//        listView.setAdapter(adapter);

         int[] arrayImages = new int[]{R.drawable.meinv1,
                R.drawable.meinv2,R.drawable.meinv3,R.drawable.meinv4};
        String[] ctype1 = new String[]{"one","two","three","four"};
        List<Map<String,Object>> listitem = new ArrayList<>();
        for (int i = 0 ; i < arrayImages.length ; i++){
            Map<String , Object> map = new HashMap<>();
            map.put("image",arrayImages[i]);
            map.put("name",ctype1[i]);
            listitem.add(map);
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(this,
                listitem,R.layout.seniorui_listview_wechat,
                new String[]{"name","image"},
                new int[]{R.id.id_listview_wechat_name,R.id.id_listview_wechat_image});
        ListView listView = findViewById(R.id.id_listview);
        listView.setAdapter(simpleAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String , Object> map = (Map<String, Object>) parent.getItemAtPosition(position);
                Toast.makeText(ListviewActivity.this,map.get("name").toString(),Toast.LENGTH_LONG).show();
            }
        });
    }
}
